<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>组织架构</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<body class="iframeBody">
<div class="Wrapper">
    <table class="layout2col1">
        <tbody>
            <tr>
                <td width="50%" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <span><i class="sqIco sqIco_3"></i>架构</span>
                        </div>
                        <div class="sqPanel_body" style="min-height: 300px;">
                            <div class="btnsBox">
                                <!-- <span class="sq-button sq-button-hasicon" id="addPlat_button"><i class="sqBtnIco_3"></i>新增平台</span> -->
                                <span class="sq-button sq-button-hasicon" id="remove_button"><i class="sqBtnIco_7"></i>删除</span>
                                <span class="sq-button sq-button-hasicon" id="reloadTree_btn"><i class="sqBtnIco_28"></i>更新</span>
                                <!-- <span class="sq-button sq-button-hasicon onlyicon" title="上移"><i class="sqBtnIco_9"></i></span>
                                <span class="sq-button sq-button-hasicon onlyicon" title="下移"><i class="sqBtnIco_8"></i></span>
                                <span class="sq-button sq-button-hasicon onlyicon" id="demotion_btn" title="降级"><i class="sqBtnIco_17"></i></span>
                                <span class="sq-button sq-button-hasicon onlyicon" id="upgrade_btn" title="升级"><i class="sqBtnIco_18"></i></span> -->
                            </div>
                            <ul id="tree1">
                            </ul>
                        </div>
                    </div>
                </td>
                <td width="50%" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <span><i class="sqIco sqIco_4"></i>架构详细</span>
                        </div>
                        <div class="sqPanel_body">
                            <div class="btnsBox">
                                <span class="sq-button sq-button-hasicon" id="change_btn"><i class="sqBtnIco_4"></i>修改</span>
                                <span class="sq-button sq-button-hasicon Hide" id="saveChange_btn"><i class="sqBtnIco_10"></i>保存</span>
                                <span class="sq-button sq-button-hasicon Hide" id="saveNew_btn"><i class="sqBtnIco_3"></i>保存</span>
                            </div>
                            <form id="sysOrg_form" class="Hide">
                            <input name="pid" id="pid" type="text" style="display: none;" value=""/>
                            <input name="oid" id="oid" type="text" style="display: none;" value=""/>
                                <div class="fieldset1 tabel_col tabel_col_1 clearFix">
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构名称</div>
                                        <div class="field">
                                            <input name="orgNm" id="orgNm" type="text" class="ui-textbox" value="" validate="{required:true}">
                                            <span class="requiredSymbol">*</span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构简称</div>
                                        <div class="field">
                                            <input name="orgShtNm" id="orgShtNm" type="text" class="ui-textbox" value="" validate="{required:true}">
                                            <span class="requiredSymbol">*</span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构显示名称</div>
                                        <div class="field">
                                            <input name="orgDispNm" id="orgDispNm" type="text" class="ui-textbox" value="" validate="{required:true}">
                                            <span class="requiredSymbol">*</span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构代码</div>
                                        <div class="field">
                                            <input name="orgCd" id="orgCd" type="text" class="ui-textbox" value="" validate="{required:true}">
                                            <span class="requiredSymbol">*</span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">排序编号</div>
                                        <div class="field">
                                            <input name="sortNo" id="sortNo" type="text" class="ui-textbox" value="">
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">上级架构</div>
                                        <div class="field">
                                            <input name="parOrgDispNm" id="parOrgDispNm" type="text" class="ui-textbox" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构类型</div>
                                        <div class="field">
						                    <select name="orgTypCd" id="orgTypCd" data-value=" ">
						                        <option value=" " seleted>请选择</option>
						                       <c:forEach items="${searchSelectOrgTypCdList }" var="orgTypCdList">
						                        	<option value="${orgTypCdList.key }">${orgTypCdList.value }</option>
						                        </c:forEach>
						                    </select>
						                </div>
                                    </div>
                                     <div class="fieldBox col_1_100">
                                        <div class="label">架构描述</div>
                                        <div class="field" style="width: 100%;">
                                            <textarea name="orgRem" id="orgRem" rows="4" class="l-textarea" style="width:100%"></textarea>
                                        </div>
                                    </div> 
                                </div>
                                <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                            </form>
                            <div id="sysOrg_form_t">
                                <div class="fieldset1 tabel_col tabel_col_1 clearFix">
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构名称</div>
                                        <div class="field">
                                            <span id="orgNm_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构简称</div>
                                        <div class="field">
                                        	<span id="orgShtNm_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构显示名称</div>
                                        <div class="field">
                                        	<span id="orgDispNm_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构代码</div>
                                        <div class="field">
                                        	<span id="orgCd_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">排序编号</div>
                                        <div class="field">
                                            <span id="sortNo_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">上级架构</div>
                                        <div class="field">
                                            <span id="parOrgDispNm_txt"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构类型</div>
                                        <div class="field">
                                            <span id="orgTypCd_text"></span>
                                        </div>
                                    </div>
                                    <div class="fieldBox col_1_100">
                                        <div class="label">架构描述</div>
                                        <div class="field" style="width: 100%;">
                                            <span id="orgRem_txt"></span>
                                        </div>
                                    </div> 
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>



<script type="text/javascript">

	var tree,
	    treeMenu,
	    treeMenu2,
	    actionNode,  //鼠标右击时的节点
	    Editable=true;
	$(function () {
    treeMenu = $.ligerMenu({
        top: 100,
        left: 100,
        width: 120,
        items:[
            { text: '添加子级架构', click: addChildNode, icon: 'addSubLevel' }
        ]
    });
    treeMenu2 = $.ligerMenu({
        top: 100,
        left: 100,
        width: 120,
        items:[
            { text: '添加同级架构', click: addPeerNode, icon: 'addSameLevel' },
            { text: '添加子级架构', click: addChildNode, icon: 'addSubLevel' }
           
        ]
    });
    tree = $("#tree1").ligerTree({
    	url: '<%=path%>/system/sysOrg/initList.do',
        ajaxType: 'get',
        checkbox: false,
        parentIcon: "SqFolder",
        childIcon: "SqLeaf",
        needCancel:false,
        isExpand: false,
        onContextmenu: function (node, e) {
            actionNode=node;
            var parentNode = tree.getParent(node);
            if (parentNode) {
                treeMenu.hide();
                treeMenu2.show({ top: e.pageY, left: e.pageX });
            }else{  //没有父节点，那就是根节点
                treeMenu2.hide();
                treeMenu.show({ top: e.pageY, left: e.pageX });
            };
            return false;
        },
        onSelect: onSelectTree,
       onCancelselect: function (data,target) {
        	 $("#sysOrg_form").addClass('Hide');
             $("#sysOrg_form_t").removeClass('Hide');
        }, 
        onSuccess: function (data) {
            // 初始化的时候默认选中某个树节点
           /*  var selectNode_parm = function (treeData){
                return treeData.text == '节点1';
            };
            tree.selectNode(selectNode_parm); */
            tree.selectNode(data[0]);
            //tree.selectNode(data[0].id);
        }
    });
    
    $("#change_btn").click(function(event) {
    	 if(Editable==false){
    		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
    		return;
    	};
    	var selectedTree_node=tree.getSelected();
        if (!selectedTree_node) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_BEFORE,SQ_type: "warning"});
            return;
        }
    	$("#saveChange_btn").removeClass("Hide");
    	$("#change_btn").addClass("Hide");
        changeForm();
        setLigerForm();
        var form = new liger.get("sysOrg_form");
   	 	form.setData({  //重置select等特殊表单
	            "orgTypCd": " ",
	        });
        $.ajax({
  			type: 'get',
  			dataType:'json',
  			data:{id:idEdit},
  			url: '<%=path%>/system/sysOrg/getById.do',
  			success:function(msg){
  				 if(msg.success == 1){
  					setValueToInput(msg.data)
  				 }else{
  				 }
  			}
  		});
        
    });
    $("#remove_button").click(function(event) {
    	if(Editable==false){
    		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
    		return;
    	};
    	var selectedTree_node=tree.getSelected();
        if (!selectedTree_node) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_BEFORE,SQ_type: "warning"});
            return;
        }
        
        $.ligerDialog.confirm('连同该架构下的子架构及人员数据权限也将被删除，是否确认删除？', function (flag) {
			if(flag == false)return;
			$.ajax({
	  			type: 'get',
	  			dataType:'json',
	  			data:{id:selectedTree_node.data.id},
	  			url: '<%=path%>/system/sysOrg/removeById.do',
	  			success:function(msg){
	  				 if(msg.success == 1){
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
	  					tree.reload();
	  					$("#resetBtn").click();
	  					 
	  				 }else{
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
	  				 }
	  			}
	  		});
		});
        
        
        
        
    });
    $("#addPlat_button").click(function(event) {
    	 $("#resetBtn").click();
    	swithBtn()
        changeForm();
    	 setLigerForm();
    	 var form = new liger.get("sysOrg_form");
    	 form.setData({  //重置select等特殊表单
	            "orgTypCd": " ",
	        });
    });
    
    $("#reloadTree_btn").click(function(event) {
        tree.reload();
    });
   
    $("#saveNew_btn").click(function(event) {
    	
    	var form = new liger.get("sysOrg_form");
        if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
  	    var data = form.getData();
  	    
  	   // alert(JSON.stringify(data));
  	   
  	    $.ajax({
  			type: 'post',
  			dataType:'json',
  			data:data,
  			url: '<%=path%>/system/sysOrg/add.do',
  			success:function(msg){
				$(this).addClass("Hide");
		    	$(this).siblings(".sq-button").removeClass("Hide");
  				 if(msg.success == 1){
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
  					 var node = msg.data;
  					 tree.reload(function(){
  						setExpand2Parents (node, "tree1");
  		                tree.selectNode(node);
  					 });
  				 }else{
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
  				 }
  			}
  		});
    });
    var idEdit;
    function changeForm () {
        $("#sysOrg_form_t").addClass('Hide');
        $("#sysOrg_form").removeClass('Hide');
        setForm();
       
    }
    function detailForm () {
        $("#sysOrg_form").addClass('Hide');
        $("#sysOrg_form_t").removeClass('Hide');
    }
    function onSelectTree (node) {
    	$("#saveNew_btn").addClass("Hide");
    	$("#change_btn").removeClass("Hide");
        //console.log(node.data);
        //alert(JSON.stringify(node));
        detailForm ()
        
         $.ajax({
  			type: 'get',
  			dataType:'json',
  			data:{id:node.data.id},
  			url: '<%=path%>/system/sysOrg/getById.do',
  			success:function(msg){
  				 if(msg.success == 1){
  					setValueToText(msg.data);
  					idEdit =msg.data.id;
  					isEditable(msg.data.isSysDef);
  					 
  				 }else{
  					 
  				 }
  			}
  		});
    }
    /*
    	是否系统内置参数获取
    */
    function isEditable(isSysDef){
    	if(isSysDef==0){
    		Editable=true;
    	}else{
    		Editable=false;
    	}
    }
    function treeMenu_handler_1 (item, i) {
        //alert(item.text+ "\n" +JSON.stringify(item)+ "\n" +actionNode.data.text+ "\n" +JSON.stringify(actionNode));
        console.log(item, i, actionNode);
    }
    //添加子级架构
    function addChildNode (){
    	 $("#resetBtn").click();
    	 swithBtn();
    	var id = actionNode.data.id;
    	var text=actionNode.data.text;
    	changeForm();
    	setLigerForm();
    	var form = new liger.get("sysOrg_form");
   	 	form.setData({  //重置select等特殊表单
	            "orgTypCd": " ",
	        });
    	$("#parOrgDispNm").val(text);
    	$("#pid").val(id);
    	
    	
    }
  //添加同级架构
    function addPeerNode (){
    	 $("#resetBtn").click();
    	 swithBtn();
    	var pid = actionNode.data.pid;
    	changeForm();
    	setLigerForm();
    	var form = new liger.get("sysOrg_form");
   	 	form.setData({  //重置select等特殊表单
	            "orgTypCd": " ",
	        });
    	  $.ajax({
    			type: 'get',
    			dataType:'json',
    			data:{id:pid},
    			url: '<%=path%>/system/sysOrg/getById.do',
    			success:function(msg){
    				 if(msg.success == 1){
    					 $("#parOrgDispNm").val(msg.data.orgNm);
    				 }
    			}
    		});
    	  $("#pid").val(pid);
    }
  	//修改中的保存按钮
     $("#saveChange_btn").click(function(){
    	 if(Editable==false){
     		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
     		return;
     	};
    	var form = new liger.get("sysOrg_form");
    	if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
  	    var data = form.getData();
  	    
  	   // alert(JSON.stringify(data));
  	   data["id"] = $("#oid").val();
  	    $.ajax({
  			type: 'post',
  			dataType:'json',
  			data:data,
  			url: '<%=path%>/system/sysOrg/modify.do',
				success : function(msg) {
					if (msg.success == 1) {
						$("#saveChange_btn").addClass("Hide");
						$("#change_btn").removeClass("Hide");
						top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						var node = $("#oid").val()
						tree.reload(function() {
							setExpand2Parents(node, "tree1");
							tree.selectNode(node);
						});

					} else {
						top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					}
				}
			});
		});

		function setForm() {
			$("#sysOrg_form").ligerForm({
				validate : true
			});
		}
		function swithBtn() {
			$("#saveNew_btn").removeClass("Hide");
			$("#saveNew_btn").siblings(".sq-button").addClass("Hide");
		}
		//赋值方法到详细
		function setValueToText(data) {
			$("#orgNm_txt").text(isNulltoNBSP(data.orgNm));
			$("#orgShtNm_txt").text(isNulltoNBSP(data.orgShtNm));
			$("#orgDispNm_txt").text(isNulltoNBSP(data.orgDispNm));
			$("#orgCd_txt").text(isNulltoNBSP(data.orgCd));
			$("#sortNo_txt").text(isNulltoNBSP(data.sortNo));
			$("#parOrgDispNm_txt").text(isNulltoNBSP(data.pidNm));
			$("#orgTypCd_text").text(isNulltoNBSP(data.orgTypCdNm));
			$("#orgRem_txt").text(isNulltoNBSP(data.orgRem));
		}
		//赋值方法到输入框
		function setValueToInput(data) {
			$("#oid").val(data.id);
			var form = new liger.get("sysOrg_form");
			form.setData({
				orgTypCd : data.orgTypCd,
			});
			$("#orgNm").val(isNulltoNBSP(data.orgNm));
			$("#orgShtNm").val(isNulltoNBSP(data.orgShtNm));
			$("#orgDispNm").val(isNulltoNBSP(data.orgDispNm));
			$("#sortNo").val(isNulltoNBSP(data.sortNo));
			$("#orgCd").val(isNulltoNBSP(data.orgCd));
			$("#parOrgDispNm").val(isNulltoNBSP(data.pidNm));
			$("#orgRem").val(isNulltoNBSP(data.orgRem));

		}
		function isNulltoNBSP(a) {
			if (a == null || a == undefined) {
				return ""
			} else {
				return a;
			}
		}
		function setLigerForm() {
			var ligerForm = liger.get("sysOrg_form")
			if (ligerForm) {
				return ligerForm;
			}
			$("#sysOrg_form").ligerForm({
				validate : true
			});
		}
		;
		function setExpand2Parents(nodeid, treeid) {
			var tree = liger.get("tree1");
			var parent = tree.getParent(tree.getDataByID(nodeid));
			if (parent != null) {
				parent.isExpand = true;
				setExpand2Parents(parent.id);
			} else {
				tree.setData(tree.getData());
				console.log(tree.getData());
			}
			;
		}
	});
</script>
</body>
</html>